﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.WordAction
{
    public partial class InterviewControl : UserControl
    {
        private WordActionService.WordActionClient _client = null;
        public InterviewControl()
        {
            InitializeComponent();
            _client = new WordActionService.WordActionClient();
            _client.GetInterViewTempleteCompleted += (sender, e1) =>
            {
                dg.ItemsSource = null;
                dg.ItemsSource = e1.Result;
            };
            this.Loaded += (sender, e) =>
            {
                _client.GetInterViewTempleteAsync("1=1");
            };
            _client.DeleteInterviewTempleteCompleted += (sender, e) =>
            {
                _client.GetInterViewTempleteAsync("1=1");
            };
            _client.ExportInterviewInfoCompleted += (sender, e) =>
            {
                //保存结果
                if (e.Error == null)
                {
                    string serverpath = _client.InnerChannel.Via.AbsoluteUri.Replace(_client.InnerChannel.Via.AbsolutePath, "");
                    System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(serverpath + e.Result, UriKind.RelativeOrAbsolute));
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败,可能原因:\n" + e.Error);
                }
            };
        }

        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            InterviewTemplete2Window window = new InterviewTemplete2Window(0);
            window.Show();
            window.Closed += new EventHandler(window_Closed);
        }

        void window_Closed(object sender, EventArgs e)
        {
            _client.GetInterViewTempleteAsync("1=1");
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as WordActionService.InterviewTemplete;
                InterviewTemplete2Window window = new InterviewTemplete2Window(item.ID);
                window.Show();
                window.Closed += new EventHandler(window_Closed);
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选中编辑项");
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click_2(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认删除选中项?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                cm.Show();
                cm.OKButton.Click += (sender1, e1) =>
                {
                    var item = dg.SelectedItem as WordActionService.InterviewTemplete;
                    _client.DeleteInterviewTempleteAsync(item.ID);
                };
               
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选中删除项");
            }
        }

        /// <summary>
        /// 导出测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _client.ExportInterviewInfoAsync(new System.Collections.ObjectModel.ObservableCollection<string>()
            { "陈劼", "3月 12 日（周一）上午8：00", "徐汇区委党校（上海市徐汇区钦州南路567号）二楼218会议室" }, 1);
        }
    }
}
